package com.water.web;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 随机验证码
 * 
 * @author whl
 * 
 */
public class PictureCheckCode {

	/**
	 * 随机生成RGB颜色
	 * 
	 * @param s
	 * @param e
	 * @return
	 */
	public static Color getRandColor(int s, int e) {
		Random random = new Random();
		if (s > 255)
			s = 255;
		if (e > 255)
			e = 255;
		int r = s + random.nextInt(e - s);// 随机生成RGB颜色的r值
		int g = s + random.nextInt(e - s);// 随机生成RGB颜色的g值
		int b = s + random.nextInt(e - s);// 随机生成RGB颜色的b值
		return new Color(r, g, b);
	}

	/**
	 * 生成随机验证码
	 * 
	 * @param request
	 * @param response
	 */
	public static BufferedImage createPicture(HttpServletRequest request,
			HttpServletResponse response) {
		// 设置不缓存图片
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0);
		// 指定生成的响应图片
		response.setContentType("image/jpeg");
		int width = 86;// 指定生成验证码图片的宽度
		int height = 22;// 指定生成验证码图片的高度
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		Graphics2D g2d = (Graphics2D) g;// 创建Graphics2D对象
		Random random = new Random();
		Font mFont = new Font("黑体", Font.BOLD, 16);// 定义字体样式
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);// 绘制背景
		g.setFont(mFont);// 设置字体
		g.setColor(getRandColor(180, 200));
		// 绘制100跟颜色和位置全部为随机产生的线条，该线条为2f
		for (int i = 0; i < 100; i++) {
			int x = random.nextInt(width - 1);
			int y = random.nextInt(height - 1);
			int x1 = random.nextInt(6) + 1;
			int y1 = random.nextInt(12) + 1;
			BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT,
					BasicStroke.JOIN_BEVEL);
			Line2D line = new Line2D.Double(x, y, x + x1, y + y1);
			g2d.setStroke(bs);
			g2d.draw(line);// 绘制直线
		}

		// 输出有英文、数字和中文随机组成的验证文字，具体的组合方式根据生产随机数确定
		String sRand = "";
		// 输出随机的验证文字
		String ctmp = "";
		int itmp = 0;
		for (int i = 0; i < 4; i++) {
			switch (random.nextInt(4)) {
			case 1:
				itmp = random.nextInt(26) + 65;// 生产A~Z的字母
				ctmp = String.valueOf((char) itmp);
				break;
			case 2:
				// 生产汉字
				String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
						"9", "a", "b", "c", "d", "e", "f" };
				// 生产第一位的区码
				int r1 = random.nextInt(3) + 11;// 生产11！14之间的随机数
				String str_r1 = rBase[r1];
				// 生产第2位的区码
				int r2;
				if (r1 == 13) {
					r2 = random.nextInt(7);// 生产0~7之间的随机数
				} else {
					r2 = random.nextInt(16);// 生产0~16之间的随机数
				}
				String str_r2 = rBase[r2];
				// 生产第1位的位码
				int r3 = random.nextInt(6) + 10;// 生产10~16之间的随机数
				String str_r3 = rBase[r3];
				// 生产第2位的位码
				int r4;
				if (r3 == 10) {
					r4 = random.nextInt(15) + 1;// 生产1~16之间的随机数
				} else if (r3 == 15) {
					r4 = random.nextInt(15);// 生产0~15之间的随机数
				} else {
					r4 = random.nextInt(16);// 生产0~16之间的随机数
				}
				String str_r4 = rBase[r4];
				// 将生产的机内码转换为汉字
				byte[] bytes = new byte[2];
				// 将生产的区码保存到字节数组的第1个元素中
				String str_r12 = str_r1 + str_r2;
				int tempLow = Integer.parseInt(str_r12, 16);
				bytes[0] = (byte) tempLow;
				// 将生成的位码保存到字节数组的第2个元素中
				String str_r34 = str_r3 + str_r4;
				int tempHigh = Integer.parseInt(str_r34, 16);
				bytes[1] = (byte) tempHigh;
				ctmp = new String(bytes);// 根据字节数组生成汉字
				break;
			default:
				itmp = random.nextInt(10) + 48;// 生产0~9的数字
				ctmp = String.valueOf((char) itmp);
				break;
			}
			sRand += ctmp;
			Color color = new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110));
			g.setColor(color);
			// 将生产的随机数进行随机缩放并旋转指定角度
			// 将文字旋转指定角度
			Graphics2D g2d_word = (Graphics2D) g;
			AffineTransform trans = new AffineTransform();
			trans.rotate(random.nextInt(45) * 3.14 / 180, 15 * i + 8, 7);
			// 缩放文字
			float scaleSize = random.nextFloat() + 0.8f;
			if (scaleSize > 1f)
				scaleSize = 1f;
			trans.scale(scaleSize, scaleSize);
			g2d_word.setTransform(trans);
			g.drawString(ctmp, 15 * i + 18, 14);
		}
		// 将生产的验证码保存到Session中
		HttpSession session = request.getSession(true);
		session.setAttribute("randCheckCode", sRand);
		// 输出生产的验证码图片
		g.dispose();
		// try {
		// ImageIO.write(image,"JPEG",response.getOutputStream());
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		return image;
	}
}
